package com.lzy.cloud.goods.service.carRent.Impl;

import com.lzy.cloud.goods.dtos.CarCalendarDto;
import com.lzy.cloud.goods.entity.CarCalendar;
import com.lzy.cloud.goods.mapper.CarCalendarMapperExt;
import com.lzy.cloud.goods.service.carRent.CarCalendarService;
import com.lzy.framework.core.utils.DateUtil;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.core.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CarCalendarServiceImpl implements CarCalendarService {

    @Autowired
    CarCalendarMapperExt carCalendarMapperExt;

    @Override
    public int insertSelective(CarCalendarDto carCalendarDto) {
        return carCalendarMapperExt.insertSelective(DozerUtils.map(carCalendarDto,CarCalendar.class));
    }

    @Override
    public List<CarCalendarDto> selectOneMonthList(CarCalendarDto carCalendarDto) {
        String yearMonthStr = "";
        if(StringUtil.isNotEmpty(carCalendarDto.getYearMonthStr())){
            yearMonthStr = carCalendarDto.getYearMonthStr();
        }
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        //获取上个月的第一天
        Date date1 = new Date();
        try {
            date1 = DateUtil.formatMonthStrToDate(yearMonthStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date1);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        carCalendarDto.setStartDate(dft.format(calendar.getTime()));

        //获取下一个月的最后一天
        calendar.add(Calendar.MONTH, 2);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        carCalendarDto.setEndDate(dft.format(calendar.getTime()));
        return carCalendarMapperExt.selectOneMonthList(carCalendarDto);
    }

    @Override
    public Boolean updateByCarIdAndOneDate(CarCalendarDto carCalendarDto) {
        if(carCalendarDto.getUpdateType() == 2){
            return carCalendarMapperExt.insertSelective(DozerUtils.map(carCalendarDto,CarCalendar.class))>0;
        }else {
            return carCalendarMapperExt.updateByCarIdAndOneDate(carCalendarDto) > 0;
        }
    }

    @Override
    public Boolean updateByCarIdAndDates(CarCalendarDto carCalendarDto) {
        return carCalendarMapperExt.updateByCarIdAndDates(carCalendarDto)>0;
    }

    @Override
    public CarCalendarDto findByCarId(CarCalendarDto carCalendarDto) {
        return carCalendarMapperExt.findByCarId(carCalendarDto);
    }

    @Override
    public int updateCarCalendar(CarCalendarDto record) {
        return carCalendarMapperExt.updateCarCalendar(record);
    }

    @Override
    public List<CarCalendarDto> priceList(CarCalendarDto carCalendarDto) {
        return carCalendarMapperExt.priceList(carCalendarDto);
    }

    @Override
    public int updateCalendarTypeByCarIdAndDate(CarCalendarDto carCalendarDto) {
        return 0;
    }

    @Override
    public List<CarCalendarDto> findCalendarByCarIdAndDate(CarCalendarDto carCalendarDto) {
        return null;
    }

    @Override
    public int updateByCarIdAndVacations(CarCalendarDto carCalendarDto) {
        return 0;
    }

    @Override
    public List<CarCalendarDto> priceWeekList(CarCalendarDto carCalendarDto) {
        return null;
    }

    @Override
    public Integer selectVacationsAndDate(CarCalendarDto carCalendarDto) {
        return null;
    }

    @Override
    public CarCalendarDto selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public BigDecimal selectCarPrice(CarCalendarDto carCalendarDto) {
        return null;
    }

    @Override
    public Integer findExistCalendar(Long carId) {
        return null;
    }

    @Override
    public int delScheduleByCarId(Long carId) {
        return 0;
    }

    @Override
    public void delByDate(CarCalendarDto dto) {

    }

    @Override
    public CarCalendarDto selectByCarIdAndDate(CarCalendarDto dto) {
        return carCalendarMapperExt.selectByCarIdAndDate(dto);
    }

    @Override
    public Boolean updateByWeekdaysAndDate(CarCalendarDto carCalendarDto,String weekdays) {

        return carCalendarMapperExt.updateByWeekdaysAndDate(carCalendarDto, weekdays)>0;
    }

    @Override
    public Boolean delById(Long id) {
        return carCalendarMapperExt.deleteByPrimaryKey(id) > 0;
    }
}
